package com.shenfa.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.shenfa.bean.Fodder;
import com.shenfa.bean.Person;
import com.shenfa.service.FodderService;
import com.shenfa.tools.SteelDate;

import com.shenfa.util.ResultObject;

/**
 * 素材管理ACTION
 * 
 * @author yanglong
 *
 */
@SuppressWarnings("serial")
public class FodderAction extends BaseAction implements ServletRequestAware{

	@Autowired
	private FodderService fodderService;
	private HttpServletRequest request;
	private Fodder fodder;
	private static Logger log=Logger.getLogger(FodderAction.class);
	private int start;
	private int limit;
	
	/**
	 * 根据对象条件查询列表,带分页
	 * @return	
	 */
	@SuppressWarnings("unchecked")
	public String list(){
		log.debug("start="+start+",limit="+limit);
		if(null!=fodder ){
			if("".equals(fodder.getType()) || "3".equals(fodder.getType())|| "4".equals(fodder.getType())){
				fodder.setType(null);
			}
			if("".equals(fodder.getState())){
				fodder.setState(null);
			}
		}
		List<Fodder> list = fodderService.queryList(fodder, start, limit);
		result = ResultObject.build("0", list);
		result.put("total",fodderService.queryCount(fodder));
		return SUCCESS;
	}
	/**
	 * 根据对象条件查询列表,不带分页
	 * @return	
	 */
	
	public String listNo(){
		log.debug("fodder="+fodder);
		if(null!=fodder && null!=fodder.getType()){
			if("3".equals(fodder.getType())|| "4".equals(fodder.getType())){
				fodder.setType(null);
			}
		}
		List<Fodder> list = fodderService.queryListNo(fodder);
		result = ResultObject.build("0", list);
		return SUCCESS;
	}
	/**
	 * 根据对象条件查询列表,不带分页
	 * @return	
	 */
	@SuppressWarnings("unchecked")
	public String listsh(){
		log.debug("start="+start+",limit="+limit+",fodder="+fodder);
		if(null!=fodder ){
			if("".equals(fodder.getType())){
				fodder.setType(null);
			}
			if("".equals(fodder.getState())){
				fodder.setState(null);
			}
		}
		List<Fodder> list = fodderService.queryListsh(fodder, start, limit);
		result = ResultObject.build("0", list);
		result.put("total",fodderService.queryCountsh(fodder));
		return SUCCESS;
	}
	
	/**
	 * 添加素材
	 */
	public String add(){
		Person person;
		Object obj=request.getSession().getAttribute("login");
		if(null==obj){
			return SUCCESS;
		}
		person=(Person)obj;
		log.debug("begin="+fodder.getBegin()+",docName="+fodder.getDocname()+",end="+fodder.getEnd()+",id="+fodder.getId()+",name="+fodder.getName()
				+",remark="+fodder.getRemark()+",state="+fodder.getState()+",type="+fodder.getType()+",url="+fodder.getUrl());
		fodder.setPersonno(person.getId());
		fodder.setCreatetime(SteelDate.getDate(SteelDate.YYYY_MM_DD_HH_MM_SS, new Date()));
		int type = fodderService.add(fodder);
		if(type == 1){
			result = ResultObject.build(String.valueOf(type), "添加成功！");
		} else {
			result = ResultObject.build(String.valueOf(type), "操作失败！");
		}
		return SUCCESS;
	}
	
	/**
	 * 修改素材
	 * @return
	 */
	public String upd(){
		log.debug("begin="+fodder.getBegin()+",docName="+fodder.getDocname()+",end="+fodder.getEnd()+",id="+fodder.getId()+",name="+fodder.getName()
				+",remark="+fodder.getRemark()+",state="+fodder.getState()+",type="+fodder.getType()+",url="+fodder.getUrl());

		int type = fodderService.upd(fodder);
		if(type == 1){
			result = ResultObject.build(String.valueOf(type), "添加成功！");
		} else {
			result = ResultObject.build(String.valueOf(type), "操作失败！");
		}
		return SUCCESS;
	}
	
	public String sh(){

		int type = fodderService.upd(fodder);
		if(type == 1){
			result = ResultObject.build(String.valueOf(type), "添加成功！");
		} else {
			result = ResultObject.build(String.valueOf(type), "操作失败！");
		}
		return SUCCESS;
	}
	
	
	/**
	 * 删除素材
	 * @return	
	 */
	public String del(){
		log.debug("id="+fodder.getId());
		int type = fodderService.del(fodder);
		if(type == 1){
			result = ResultObject.build(String.valueOf(type), "添加成功！");
		} else {
			result = ResultObject.build(String.valueOf(type), "操作失败！");
		}
		return SUCCESS;
	}

	public Fodder getFodder() {
		return fodder;
	}

	public void setFodder(Fodder fodder) {
		this.fodder = fodder;
	}
	
	public void setStart(int start) {
		this.start = start;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public void setServletRequest(HttpServletRequest arg0) {
		this.request=arg0;
	}
	
}
